<template>
<div>
  <el-form   :model="pageForm" label-width="80px" :rules="pageFormRules" ref="pageForm" >
      <el-form-item label="所属站点" prop="siteId">
        <el-select v-model="pageForm.siteId" placeholder="请选择站点">
          <el-option
            v-for="item in siteList"
            :key="item.siteId"
            :label="item.siteName"
            :value="item.siteId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="选择模版" prop="templateId">
        <el-select v-model="pageForm.templateId" placeholder="请选择">
          <el-option
            v-for="item in templateList"
            :key="item.templateId"
            :label="item.templateName"
            :value="item.templateId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="页面名称" prop="pageName">
        <el-input v-model="pageForm.pageName" auto-complete="off" ></el-input>
      </el-form-item>

      <el-form-item label="别名" prop="pageAliase">
        <el-input v-model="pageForm.pageAliase" auto-complete="off" ></el-input>
      </el-form-item>
      <el-form-item label="访问路径" prop="pageWebPath">
        <el-input v-model="pageForm.pageWebPath" auto-complete="off" ></el-input>
      </el-form-item>

      <el-form-item label="物理路径" prop="pagePhysicalPath">
        <el-input v-model="pageForm.pagePhysicalPath" auto-complete="off" ></el-input>
      </el-form-item>
      <el-form-item label="数据Url" prop="dataUrl">
        <el-input v-model="pageForm.dataUrl" auto-complete="off" ></el-input>
      </el-form-item>

      <el-form-item label="类型">
        <el-radio-group v-model="pageForm.pageType">
          <el-radio class="radio" label="0">静态</el-radio>
          <el-radio class="radio" label="1">动态</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker type="datetime" placeholder="创建时间" v-model="pageForm.pageCreateTime"></el-date-picker>
      </el-form-item>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click.native="addSubmit">提交</el-button>
      <el-button type="primary" @click="resetForm">重置</el-button>
      <el-button @click="goBack">返回</el-button>
    </div>
</div>
</template>
<script>
  import * as cmsApi from '../api/cms';
  export default {
    data() {
      return {
        //模版列表
        templateList:[],
        addLoading: false,//加载效果标记
        //新增界面数据
        pageForm: {
          siteId:'',
          templateId:'',
          pageName: '',
          pageAliase: '',
          pageWebPath: '',
          dataUrl:'',
          pageParameter:'',
          pagePhysicalPath:'',
          pageType:'',
          pageCreateTime: new Date()
        },
        pageFormRules: {
          siteId:[
            {required: true, message: '请选择站点', trigger: 'blur'}
          ],
          templateId:[
            {required: true, message: '请选择模版', trigger: 'blur'}
          ],
          pageName: [
            {required: true, message: '请输入页面名称', trigger: 'blur'}
          ],
          pageWebPath: [
            {required: true, message: '请输入访问路径', trigger: 'blur'}
          ],
          pagePhysicalPath: [
            {required: true, message: '请输入物理路径', trigger: 'blur'}
          ]
        },
        siteList:[]
      }
    },
    methods: {
      // 查询站点列表
      querySite: function(){
        cmsApi.site_list().then((res)=>{
          // 将结果赋值
          this.siteList = res;
        })
      },
      // 查询模板列表
      queryTemplate: function(){
        cmsApi.template_list().then((res)=>{
          // 将结果赋值
          this.templateList = res;
        })
      },
      // 返回上一页，需要带上之前的参数
      goBack(){
        // 获取路由中的参数，返回时带上
        this.$router.push({
          path:'/cms/page/list',
          query:{
            page:this.$route.query.page,
            siteId:this.$route.query.siteId,
            pageAliase:this.$route.query.pageAliase
          }
        })
      },

      // 提交
      addSubmit(){
        // 首先验证表单
        this.$refs["pageForm"].validate((res) => {
          if(res){
            // 提交
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              cmsApi.page_add(this.pageForm).then(res => {
                if(res.success){
                  // 添加成功，提示
                  this.$message.success("新增成功！");
                  // 然后清除表单
                  this.resetForm();
                }else{
                  this.$message.error(res.message);
                }
              })
            })
          }
        })
      },

      // 清除表单
      resetForm(){
        this.$refs["pageForm"].resetFields();
      }
    },
    mounted() {
      //初始化站点列表
      this.querySite();
      this.queryTemplate();
      // this.siteList = [
      //   {
      //     siteId:'5a751fab6abb5044e0d19ea1',
      //     siteName:'门户主站'
      //   }
      // ]
      // //模板列表
      // this.templateList = [
      //   {
      //     templateId:'5a962b52b00ffc514038faf7',
      //     templateName:'首页'
      //   },
      //   {
      //     templateId:'5e4fcf45392c02403c3fbbcc',
      //     templateName:'轮播图'
      //   }
      // ]
    },
  }
</script>
<style>
  /* 样式，可写可不写 */
</style>